#pragma once

#include <cstring>
#include <string>
#include <netinet/in.h>
#include <arpa/inet.h>

class EndPoint
{
public:
    explicit EndPoint(const char* ip, unsigned short port)
    {
        bzero(&addr, sizeof addr);
        addr.sin_family = AF_INET;
        int ret = inet_pton(AF_INET, ip, &addr.sin_addr);
        addr.sin_port = htons(port);
    }
    explicit EndPoint(const struct sockaddr_in& addr_) : addr(addr_) {}
    struct sockaddr* GetAddr() const
    {
        return (struct sockaddr*)&addr;
    }
    unsigned int GetAddrLen() const
    {
        return sizeof addr;
    }
    std::string GetIp()
    {
        char buf[64];
        bzero(buf, sizeof buf);
        inet_ntop(addr.sin_family, (void*)&addr.sin_addr, buf, sizeof buf);
        return std::string(buf);
    }
    unsigned short GetPort()
    {
        return ntohs(addr.sin_port);
    }
private:
    struct sockaddr_in addr;
};